package com.bawei.lookfor2204a.view.fragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import coil.load
import coil.transform.CircleCropTransformation
import com.bawei.lookfor2204a.R
import com.bawei.lookfor2204a.base.BaseViewFragment
import com.bawei.lookfor2204a.databinding.FragmentWoDeBinding
import com.bawei.lookfor2204a.view.activity.login.LoginActivity
import com.blankj.utilcode.util.SPUtils

class WoDeFragment : BaseViewFragment<FragmentWoDeBinding>(R.layout.fragment_wo_de) {
    override fun initData() {
    }

    override fun initView() {
        // 未登录业务处理
        binding.tvLogin.setOnClickListener {
            startActivity(Intent(activity, LoginActivity::class.java))  // 跳转到登录页面
        }
    }

    override fun onResume() {
        super.onResume()
        val sessionId = SPUtils.getInstance().getString("sessionId")
        if (sessionId.isEmpty()) {
            // 未登录
            binding.rlNoLogin.visibility = View.VISIBLE
            binding.rlLogin.visibility = View.GONE
        } else {
            // 已登录
            binding.rlNoLogin.visibility = View.GONE
            binding.rlLogin.visibility = View.VISIBLE

            // 读取本地存储的用户头像
            val headPic = SPUtils.getInstance().getString("headPic")
            // 读取本地存储的用户昵称
            val nickName = SPUtils.getInstance().getString("nickName")
            // 显示用户用户头像
            binding.ivTou.load(headPic){
                // 圆形图片
                transformations(CircleCropTransformation())
                // 占位图
                placeholder(R.mipmap.ic_launcher)
                // 错误图
                error(R.mipmap.ic_launcher)
            }
            // 显示用户昵称
            binding.tvNickname.text = nickName
        }
    }

}